package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchOrg;
import nsu.edu.zsq.bean.Teacher;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.SchOrgService;
import nsu.edu.zsq.service.sys.TeacherService;
import nsu.edu.zsq.util.UploadFileUtil;

/** 
* Description: 教师的controller类<br>
*/
@RequestMapping("/sys/teacher/info")
@Controller
public class TeacherController {
    
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SchOrgService schOrgService;
    
    /**
     * 获取学校院系的树结构数据以及院系列表
     * @return JsonData
     */
    @RequestMapping("/listSchOrgWithTree")
    @ResponseBody
    public JsonData listSchOrgWithTree() {
        String listSchOrgWithTree = schOrgService.listSchOrgParamWithTree();
        List<SchOrg> listSchOrg = schOrgService.listSchOrg();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithTree", listSchOrgWithTree);
        map.put("listSchOrg", listSchOrg);
        return JsonData.success(map);
    }
    
    /**
     * 根据院系id获取教师的分页数据
     * @param orgId 院系id
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listTeacher")
    @ResponseBody
    public JsonData listTeacherWithPage(@RequestParam("orgId") int orgId, PageQuery pageQuery) {
        PageInfo<Teacher> listTeacher = teacherService.listTeacherWithPage(orgId, pageQuery);
        return JsonData.success(listTeacher);
    }
    
    
    /**
     * 跳转到添加教师信息页面
     * @param orgId 院系id
     * @param model 
     * @return String manager/teacher/teacher_add.html
     */
    @RequestMapping("/toAddTeacher")
    public String toAddTeacher(@RequestParam("orgId")int orgId, Model model) {
        model.addAttribute("orgId", orgId);
        return "manager/teacher/teacher_add.html";
    }
    
    /**
     * 跳转到编辑教师信息页面
     * @param teacherId 教师id
     * @param model 
     * @return String manager/teacher/teacher_edit.html
     */
    @RequestMapping("/toEditTeacher")
    public String toEditTeacher(@RequestParam("teacherId")int teacherId, Model model) {
        model.addAttribute("teacherId", teacherId);
        return "manager/teacher/teacher_edit.html";
    }
    
    /**
     * 获取一条教师信息（用于编辑页面）
     * @param teacherId 教师id
     * @return JsonData 
     */
    @RequestMapping("/getTeacher")
    @ResponseBody
    public JsonData getTeacher(@RequestParam("teacherId")int teacherId) {
        Teacher teacher = teacherService.getTeacher(teacherId);
        return JsonData.success(teacher);
    }
    
    /**
     * 保存一条教师信息
     * @param teacher 教师信息
     * @return JsonData
     */
    @RequestMapping("/saveTeacher")
    @ResponseBody
    public JsonData saveTeacher(Teacher teacher) {
        teacherService.saveTeacher(teacher);
        return JsonData.success();
    }
    
    /**
     * 修改一条教师信息
     * @param teacher 教师信息
     * @return JsonData
     */
    @RequestMapping("/editTeacher")
    @ResponseBody
    public JsonData editTeacher(Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除教师
     * @param teacherId 教师id数组
     * @return JsonData
     */
    @RequestMapping("/delTeacher")
    @ResponseBody
    public JsonData delTeacher(@RequestParam("teacherId[]")int teacherId[]) {
        if (teacherId.length > 1) {
            // 批量删除
            teacherService.deleteBatchTeacher(teacherId);
        } else if (teacherId.length == 1) {
            // 单个删除
            teacherService.deleteTeacher(teacherId[0]);
        } else {
            throw new ParamException("teacherId无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 从excel中导入教师数据
     * @param excelFile
     * @return JsonData
     */
    @RequestMapping("/importTeacher")
    @ResponseBody
    public JsonData importTeacher(@RequestParam("excelFile")MultipartFile excelFile) {
        // 1.解析excel文件，保存到数据库
        teacherService.importTeacherFromExcel(excelFile);
        // 2.启动多线程保存文件（先保存文件会破坏MultipartFile数据）
        new Thread() {
            public void run() {
                UploadFileUtil.uploadFile(excelFile);
            }
        }.start();

        return JsonData.success();
    }
    
    /**
     * 导出教师数据到excel文件
     * @param response 请求
     */
    @RequestMapping("/exportTeacher")
    public void exportTeacher(HttpServletResponse response) {
        teacherService.exportTeacherToExcel(response);
    }
    
    /**
     * 重置教师登录密码
     * @param teacherId 教师id
     * @return JsonData
     */
    @RequestMapping("/resetPassword")
    @ResponseBody
    public JsonData resetPassword(@RequestParam("teacherId")int teacherId) {
        teacherService.resetPassword(teacherId);
        return JsonData.success();
    }
}
